package com.foreveredu.propms.persist.hibernate;

import java.util.List;

import com.foreveredu.propms.domain.BuildingType;
import com.foreveredu.propms.domain.ChargeCriterion;
import com.foreveredu.propms.domain.ChargeItem;
import com.foreveredu.propms.domain.House;
import com.foreveredu.propms.domain.HouseSource;
import com.foreveredu.propms.domain.HouseType;
import com.foreveredu.propms.domain.Message;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.persist.IChargeCriterionDao;

public class HibernateChargeCriterionDao extends HibernateAbstractDao<ChargeCriterion, Integer> implements IChargeCriterionDao {

	private static final long serialVersionUID = 1L;
	
	public HibernateChargeCriterionDao() {
		super(ChargeCriterion.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ChargeCriterion findCriterionByTypeAndItem(HouseType houseType,
			BuildingType buildingType, HouseSource source, ChargeItem item) {

		String hql = "from ChargeCriterion cc where cc.houseType.id=? and cc.buildingType.id=? and cc.houseSource.id=? and cc.chargeItem.id=? and cc.enabled=1";
		List<ChargeCriterion> ccList = this.getHibernateTemplate().find(hql, new Object[]{houseType.getId(),buildingType.getId(),source.getId(),item.getId()});

		ChargeCriterion cc = null;
		if(ccList != null && ccList.size()>0) {
			cc = ccList.get(0);
		}
		
		return cc;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ChargeCriterion findCriterionByHouseAndItem(House house, ChargeItem item) {
		String hql = "from ChargeCriterion cc where cc.house.id=? and cc.chargeItem.id=? and cc.type=1 and cc.enabled=1";
		List<ChargeCriterion> ccList = this.getHibernateTemplate().find(hql, new Object[]{house.getId(),item.getId()});

		ChargeCriterion cc = null;
		if(ccList != null && ccList.size()>0) {
			cc = ccList.get(0);
		}
		
		return cc;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ChargeCriterion> findSingleChargeCriterions() {
		String hql = "from ChargeCriterion cc where cc.type=1 and cc.enabled=1";
		List<ChargeCriterion> ccList = this.getHibernateTemplate().find(hql);

		return ccList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ChargeCriterion> findPrimaryChargeCriterions() {
		String hql = "from ChargeCriterion cc where cc.type=0 and cc.enabled=1";
		List<ChargeCriterion> ccList = this.getHibernateTemplate().find(hql);

		return ccList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ChargeCriterion findCriterionByHouseAndItem(House house, ChargeItem item, SubDistrict subDistrict) {
		String hql = "from ChargeCriterion cc where cc.house.id=? and cc.chargeItem.id=? and cc.type=1 and cc.enabled=1 and cc.subDistrict.id=?";
		List<ChargeCriterion> ccList = this.getHibernateTemplate().find(hql, new Object[]{house.getId(),item.getId(),subDistrict.getId()});

		ChargeCriterion cc = null;
		if(ccList != null && ccList.size()>0) {
			cc = ccList.get(0);
		}
		
		return cc;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ChargeCriterion findCriterionByTypeAndItem(HouseType houseType, BuildingType buildingType, HouseSource source, ChargeItem item, SubDistrict subDistrict) {

		String hql = "from ChargeCriterion cc where cc.houseType.id=? and cc.buildingType.id=? and cc.houseSource.id=? and cc.chargeItem.id=? and cc.enabled=1 and cc.type=0 and cc.subDistrict.id=?";
		List<ChargeCriterion> ccList = this.getHibernateTemplate().find(hql, new Object[]{houseType.getId(),buildingType.getId(),source.getId(),item.getId(),subDistrict.getId()});

		ChargeCriterion cc = null;
		if(ccList != null && ccList.size()>0) {
			cc = ccList.get(0);
		}
		
		return cc;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public PageModel findChargeCriterionsList(int offset) {
		List<ChargeCriterion> chargeCriterions = this.getHibernateTemplate().find("from ChargeCriterion cc where cc.type=0 and cc.enabled=1 order by cc.subDistrict.id desc");
		PageModel pm = new PageModel();
		pm.setTotal(chargeCriterions.size());
		if (offset + 10 > chargeCriterions.size()) {
			pm.setDatas(chargeCriterions.subList(offset, chargeCriterions.size()));
			return pm;
		}
		pm.setDatas(chargeCriterions.subList(offset, offset + 10));
		return pm;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public PageModel findSingChargeCriterionsList(int offset) {
		List<ChargeCriterion> chargeCriterions = this.getHibernateTemplate().find("from ChargeCriterion cc where cc.type=1 and cc.enabled=1 order by cc.subDistrict.id desc");
		PageModel pm = new PageModel();
		pm.setTotal(chargeCriterions.size());
		if (offset + 10 > chargeCriterions.size()) {
			pm.setDatas(chargeCriterions.subList(offset, chargeCriterions.size()));
			return pm;
		}
		pm.setDatas(chargeCriterions.subList(offset, offset + 10));
		return pm;
	}

	
}
